<template>
  <div id="meeting">
    <el-container>
      <el-header>
        <p>请假申请</p>
      </el-header>
      <el-main>
        <el-form ref="form1" :model="form" label-width="80px">
          <el-descriptions title="发起人信息" :column="2" border>
            <el-descriptions-item label="流水号"
              ><el-input v-model="form.id" disabled></el-input
            ></el-descriptions-item>
            <el-descriptions-item label="姓名"
              ><el-input v-model="user.name" disabled></el-input
            ></el-descriptions-item>
            <el-descriptions-item label="部门"
              ><el-input v-model="user.department_name" disabled></el-input
            ></el-descriptions-item>
            <el-descriptions-item label="岗位"
              ><el-input v-model="user.post_name" disabled></el-input
            ></el-descriptions-item>
            <el-descriptions-item label="发起时间"
              ><el-input v-model="gettime" disabled></el-input
            ></el-descriptions-item>
          </el-descriptions>
          <el-descriptions title="申请信息" :column="2" border>
            <el-descriptions-item label="请假类型"
              ><el-select v-model="form.type">
                <el-option label="事假" value="1"></el-option>
                <el-option label="病假" value="2"></el-option>
                <el-option label="年假" value="3"></el-option>
                <el-option label="外勤" value="4"></el-option>
              </el-select>
            </el-descriptions-item>
            <el-descriptions-item label="请假时间">
              <el-date-picker
                v-model="form.starttime"
                type="datetime"
                placeholder="选择开始时间"
              >
              </el-date-picker>
              <el-date-picker
                v-model="form.endtime"
                type="datetime"
                placeholder="选择结束时间"
              >
              </el-date-picker
            ></el-descriptions-item>
            <el-descriptions-item label="请假事由"
              ><el-input v-model="form.reason"></el-input
            ></el-descriptions-item>
          </el-descriptions>
        </el-form>
        <div id="mybut">
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button type="info" @click="back">返回</el-button>
        </div>
      </el-main>
    </el-container>
  </div>
</template>
<script>
import axios from "@/utils/http";
export default {
  data() {
    return {
      form: {
        id: "",
        name: "0",
        type: "",
        starttime: "",
        endtime: "",
        reason: "",
      },
      gettime: "",
      zhanghao: "",
      user: "",
    };
  },
  methods: {
    liushui: function () {
      let liushuihao = "LS" + Math.ceil(Math.random() * (1999 - 1000) + 1000);
      // console.log(liushui);
      axios({
        url: "/home/apply_liushui",
        method: "post",
        data: {
          mun: liushuihao,
        },
      }).then((res) => {
        // console.log(res.data.code);
        if (res.data.code == 200) {
          this.form.id = liushuihao;
        } else if (res.data.code == 203) {
          this.liushui();
        }
      });
    },
    submit: function () {
      // let type=''
      // if(this.form.type=='1'){
      //   type='事假'
      // }else if(this.form.type=='2'){
      //   type='病假'
      // }else if(this.form.type=='3'){
      //   type='年假'
      // }else if(this.form.type=='4'){
      //   type='外勤'
      // }
      // console.log(type);

      // console.log(this.gettime);

      let starttime = this.transferTime(this.form.starttime);
      let endtime = this.transferTime(this.form.endtime);
      // console.log(starttime, endtime);
      // console.log(this.form.id);
      axios({
        url: "/home/apply_shenqing",
        method: "post",
        data: {
          user: this.user,
          mun: this.form.id,
          type: this.form.type,
          starttime: starttime,
          endtime: endtime,
          content: this.form.reason,
          applytime: this.gettime,
        },
      }).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            message: res.data.msg,
            type: "success",
          });
          this.liushui();
          this.form.type = "";
          this.form.reason = "";
          this.form.starttime = "";
          this.form.endtime = "";
        } else if (res.data.code == 203) {
          this.$message.error(res.data.msg);
        }
      });
    },
    back: function () {
      this.$router.back(-1);
    },
    time: function () {
      //获取当前时间
      // var _this = this;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf =
        new Date().getMinutes() < 10
          ? "0" + new Date().getMinutes()
          : new Date().getMinutes();
      let ss =
        new Date().getSeconds() < 10
          ? "0" + new Date().getSeconds()
          : new Date().getSeconds();
      this.gettime = yy + "-" + mm + "-" + dd + " " + hh + ":" + mf + ":" + ss;
    },
    //将计算机时间转换为中国时间
    transferTime(time) {
      if (time) {
        var d = new Date(time);
        var date =
          d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
        // " " +
        // d.getHours() +
        // ":" +
        // d.getMinutes() +
        // ":" +
        // d.getSeconds();
        return date;
      } else {
        return "";
      }
    },
  },
  mounted: function () {
    if (this.$store.state.data) {
      this.user = this.$store.state.data;
    } else {
      this.zhanghao = JSON.parse(sessionStorage.getItem("user")).account;
      this.user = JSON.parse(sessionStorage.getItem("user"));
    }
    this.liushui();
    this.time();
    setInterval(() => {
      this.time();
    }, 1000);
  },
  computed: {},
};
</script>
<style scoped lang='less'>
.el-main {
  padding: 20px 0 !important;
}
#meeting {
  width: 70%;
  margin: 20px auto;
  padding: 30px;
  border-radius: 5px;
}
.el-header {
  color: #666;
  text-align: center;
  line-height: 60px;
  p {
    font-weight: bold;
    font-size: 24px;
  }
}
.el-select {
  width: 100%;
}
#mybut {
  text-align: center;
  margin-top: 20px;
}
.el-date-editor {
  width: 100%;
}
.el-descriptions {
  margin-bottom: 20px !important;
}
</style>